package cn.schoolwow.workflow.module.task.action.flow.addSign;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.transaction.Transaction;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.task.type.AddSignTaskRequest;
import cn.schoolwow.workflow.entity.WorkFlowTask;
import cn.schoolwow.workflow.module.common.domain.WorkFlowTaskType;
import cn.schoolwow.workflow.module.common.flow.AddWorkFlowHistoryFlow;
import cn.schoolwow.workflow.module.task.action.flow.common.ReplaceAssignerCommonFlow;
import com.alibaba.fastjson.JSONObject;

public class AfterAddSignTaskFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        DAO dao = flowContext.checkInstanceData(DAO.class);
        dao.startTransaction(transaction -> {
            flowContext.putTemporaryInstanceData(transaction, Transaction.class);
            createAfterAssignTask(flowContext);
            completeCurrentTask(flowContext);
        });
    }

    @Override
    public String name() {
        return "后加签工作流实例任务";
    }

    private void createAfterAssignTask(FlowContext flowContext){
        WorkFlowTask workFlowTask = flowContext.checkInstanceData(WorkFlowTask.class);
        AddSignTaskRequest addSignTaskRequest = flowContext.checkInstanceData(AddSignTaskRequest.class);
        Transaction transaction = flowContext.checkInstanceData(Transaction.class);

        for(String assigner: addSignTaskRequest.signAssignerList){
            WorkFlowTask beforeSignWorkFlowTask = new WorkFlowTask();
            beforeSignWorkFlowTask.setTaskName(addSignTaskRequest.signTaskName);
            beforeSignWorkFlowTask.setInstanceId(workFlowTask.getInstanceId());
            beforeSignWorkFlowTask.setNodeId(workFlowTask.getNodeId());
            beforeSignWorkFlowTask.setType(WorkFlowTaskType.AddAfterSign.value);
            flowContext.startFlow(new ReplaceAssignerCommonFlow())
                    .putCurrentCompositeFlowData("assigner", assigner)
                    .execute();
            String filterAssigner = flowContext.checkData("assigner", String.class);
            beforeSignWorkFlowTask.setAssigner(filterAssigner);
            beforeSignWorkFlowTask.setComplete(false);
            JSONObject meta = new JSONObject();
            meta.put("auditType", addSignTaskRequest.auditType.value);
            beforeSignWorkFlowTask.setMata(meta);
            transaction.insert(beforeSignWorkFlowTask);

            flowContext.startFlow(new AddWorkFlowHistoryFlow())
                    .putCurrentCompositeFlowData("workFlowInstanceId", workFlowTask.getInstanceId())
                    .putCurrentCompositeFlowData("message", "添加后加签任务["+ addSignTaskRequest.signTaskName+"]")
                    .putCurrentCompositeFlowData("assigner", filterAssigner)
                    .execute();
        }
    }

    private void completeCurrentTask(FlowContext flowContext){
        WorkFlowTask workFlowTask = flowContext.checkInstanceData(WorkFlowTask.class);
        AddSignTaskRequest addSignTaskRequest = flowContext.checkInstanceData(AddSignTaskRequest.class);
        Transaction transaction = flowContext.checkInstanceData(Transaction.class);

        transaction.query(WorkFlowTask.class)
                .addQuery("id", addSignTaskRequest.taskId)
                .addUpdate("complete", true)
                .execute()
                .update();

        flowContext.startFlow(new AddWorkFlowHistoryFlow())
                .putCurrentCompositeFlowData("workFlowInstanceId", workFlowTask.getInstanceId())
                .putCurrentCompositeFlowData("message", "审核通过任务["+ workFlowTask.getTaskName()+"]")
                .putCurrentCompositeFlowData("assigner", workFlowTask.getAssigner())
                .execute();
    }

}
